﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRCITIZENManager : BasePage
{
    public static bool Insert;
    BUS_WRCITIZEN busWRCITIZEN = new BUS_WRCITIZEN();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
        }
    }
    private void BindGrid()
    {
        grvWRCITIZEN.DataSource = busWRCITIZEN.Get_Citizen_List();
        //grvWRCITIZEN.DataSource = busWRCITIZEN._GetList().Select(p=>new
        //{
        // p.ID,
        // p.CountryName,
        // p.CounttryCode,
        // p.Description,
        // p.Active,
        // p.Keywords,
        // CreateDate = Convert.ToDateTime(p.CreateDate).ToShortDateString()
        //}).ToList();
        grvWRCITIZEN.DataBind();
    }

    public void BindDrop()
    {
        
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtCountryName.Text = "";
        txtCounttryCode.Text = "";
        txtDescription.Text = "";
        txtKeywords.Text = "";

        BindDrop();

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRCITIZEN.Items.Count > 0)
        {
            for (int i = 0; i < grvWRCITIZEN.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRCITIZEN.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRCITIZEN.Items[i].Cells[1].Text;
                    try
                    {
                        busWRCITIZEN._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtCountryName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập CountryName!");
            txtCountryName.Focus();
            return;
        }
        if (txtCounttryCode.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập CounttryCode!");
            txtCounttryCode.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRCITIZEN obj = new WRCITIZEN();
        obj.CountryName = txtCountryName.Text;
        obj.CounttryCode = txtCounttryCode.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        obj.Description = txtDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;

        if (Insert)
        {
            obj.CreateDate = DateTime.Now;
            busWRCITIZEN._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRCITIZEN._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRCITIZEN_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRCITIZEN _WRCITIZEN = busWRCITIZEN._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtCountryName.Text = _WRCITIZEN.CountryName;
            txtCounttryCode.Text = _WRCITIZEN.CounttryCode;
            txtDescription.Text = _WRCITIZEN.Description;
            dropActive.SelectedValue = _WRCITIZEN.Active.ToString();
            txtKeywords.Text = _WRCITIZEN.Keywords;

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRCITIZEN._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRCITIZEN_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRCITIZEN.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
